<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Attend extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->jxcsys = $this->session->userdata('jxcsys');
    }
	
	public function index(){
	    $times = intval($this->input->get_post('times',TRUE));
	    if($times==1){
	        $fromDate = str_enhtml($this->input->get_post('fromDate',TRUE));
	        $toDate   = str_enhtml($this->input->get_post('toDate',TRUE));
	        $weekarray=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
	        $colModel = [];
	        $colModel[] = ['name'=>'name','label'=>'员工姓名','width'=>120,'align'=>'center'];
	        if(empty($fromDate) && empty($toDate)){
	        }else if(!empty($fromDate) && empty($toDate)){
	            $toDate = $fromDate;
	        }else if(empty($fromDate) && !empty($toDate)){
	            $fromDate = $toDate;
	        }
	        //echo $weekarray[date("w",strtotime("2018-10-6"))];
	        if(!empty($fromDate)){
	            if(strtotime($fromDate)<=strtotime($toDate)){
	                $d = $fromDate;
	                while(strtotime($d)<=strtotime($toDate)){
	                    $colModel[] = ['name'=>$d.'up','label'=>$d.'上午','width'=>120,'align'=>'center'];
	                    $colModel[] = ['name'=>$d.'dw','label'=>$d.'下午','width'=>120,'align'=>'center'];
	                    $d = date('Y-m-d',strtotime('+1 day',strtotime($d)));
	                }
	            }
	        }
	        $json['status'] = 200;
	        $json['msg']    = 'success';
	        $json['data']['colModel']      = $colModel;//[['name'=>'modifyTime','label'=>'日期','width'=>150],['name'=>'loginName','label'=>'用户名','width'=>150],['name'=>'name','label'=>'姓名','width'=>150],['name'=>'log','label'=>'日志','width'=>450]];
	        die(json_encode($json));
	    }else{
    		$fromDate = str_enhtml($this->input->get_post('fromDate',TRUE));
    		$toDate   = str_enhtml($this->input->get_post('toDate',TRUE));
    		$user = str_enhtml($this->input->get_post('user',TRUE));
    		$where['unm'] = $user ? $user :'';
    		$where['dt >='] = $fromDate ? $fromDate :''; 
    		$where['dt <='] = $toDate ? $toDate :'';                   
    		$data = $this->mysql_model->get_results('attend',array_filter($where),'id desc');  
    		$ds = array_column($data, 'tm', 'pid');
    		if(empty($fromDate) && empty($toDate)){
    		}else if(!empty($fromDate) && empty($toDate)){
    		    $toDate = $fromDate;
    		}else if(empty($fromDate) && !empty($toDate)){
    		    $fromDate = $toDate;
    		}
    		
    		$list = $this->mysql_model->get_results('admin',array_filter(['name'=>$user ? $user :'','status'=>1]),'uid desc');
    		foreach ($list as $arr=>$row) {
    		    $v[$arr]['uid']              = intval($row['uid']);
    			$v[$arr]['name']            = $row['name'];
    			if(!empty($fromDate)){
    			    if(strtotime($fromDate)<=strtotime($toDate)){
    			        $d = $fromDate;
    			        while(strtotime($d)<=strtotime($toDate)){//die(var_export($where,true));
    			            $v[$arr][$d.'up']  = isset($ds[$d.'up'.$row['uid']]) ? $ds[$d.'up'.$row['uid']] : '';
    			            $v[$arr][$d.'dw']  = isset($ds[$d.'dw'.$row['uid']]) ? $ds[$d.'dw'.$row['uid']] : '';
    			            $d = date('Y-m-d',strtotime('+1 day',strtotime($d)));
    			        }
    			    }
    			}
    		}
    		$json['status'] = 200;
    		$json['msg']    = 'success'; 
    		$json['data']['page']      = 1;                                                      
    		$json['data']['records']   = count($list);     
    		$json['data']['total']     = 1; 
    		$json['data']['rows']      = isset($v) ? $v : array();
    		die(json_encode($json));
	    }
	}
	
	public function initloglist(){
		$this->load->view('settings/log-initloglist');	
	}
	
	//用户列表
	public function queryAllUser(){ 
		$list = $this->mysql_model->get_results('admin','(1=1)','uid desc',0,0,'uid,name');  
		foreach ($list as $arr=>$row) {
		    $v[$arr]['userid']   = intval($row['uid']);
			$v[$arr]['name']     = $row['name'];
		}
		$json['status'] = 200;
		$json['msg']    = 'success';                                             
		$json['data']['totalsize']  = count($list);    
		$json['data']['items']      = isset($v) ? $v : array();
		die(json_encode($json));
	}
	
		
    //操作类型
	public function queryAllOperateType(){
		$menu = array_column($this->mysql_model->get_results('menu',array('pid'=>0),'id desc'),'title','id');     
		$list = $this->mysql_model->get_results('menu',array('depth>'=>1),'id desc');  
		foreach ($list as $arr=>$row) {
		    $v[$arr]['indexid']           = $row['id'];
			$v[$arr]['operateTypeName']   = $row['title'].isset($menu[$row['pid']]) ? $menu[$row['pid']] : '';
		}
		$json['status'] = 200;
		$json['msg']    = 'success';                                             
		$json['data']['totalsize']   = count($list);  
		$json['data']['items']       = isset($v) ? $v : array();
		die(json_encode($json));
	}
	
	public function status(){
	    $pid = str_enhtml($this->input->post('pid',TRUE));
	    $unm = str_enhtml($this->input->post('unm',TRUE));
	    $uid = intval($this->input->post('uid',TRUE));
	    if(!empty($pid)){
    	    $ds = $this->mysql_model->get_rows('attend',array('pid'=>$pid));
    	    if (count($ds) > 0) {
    	        $data['pid']=$pid;
    	        $this->mysql_model->delete('attend',$data);
    	    }else{
    	        $data['pid']=$pid;
    	        $data['uid']=$uid;
    	        $data['tm']=date('H:i:s');
    	        $data['dt']= substr($pid, 0, 10); //date('Y-m-d');
    	        $data['unm']=$unm;
    	        $this->mysql_model->insert('attend',$data);
    	    }
    	    str_alert(200,'success');
	    }
	    str_alert(-1,'操作失败');
	}
	
	public function statusSelf(){
	    $tp = str_enhtml($this->input->post('tp',TRUE));
	    $uid       = $this->jxcsys['uid'];
	    $unm       = $this->jxcsys['name'];
	    $data['dt']= date('Y-m-d');
	    $data['tm']=date('H:i:s');
	    $data['uid']=$uid;
	    $data['unm']=$unm;
	    $h=date('G');
	    if($h>12&&$tp=='up')
	        str_alert(-1,'现在非上午打卡时间');
	    if($h<=12&&$tp=='dw')
	        str_alert(-1,'现在非下午打卡时间');
	    $data['pid']= $data['dt'].$tp.$uid;
	    
	    if(!empty($uid)){
	        $ds = $this->mysql_model->get_rows('attend',array('pid'=>$data['pid']));
	        if (count($ds) > 0) {
	            str_alert(-1,'您已经打过卡了');
	        }else{
	            $this->mysql_model->insert('attend',$data);
	        }
	        str_alert(200,'success');
	    }
	    str_alert(-1,'操作失败');
	}

	public function getStatusSelf(){
	    $tp = str_enhtml($this->input->post('tp',TRUE));
	    $uid       = $this->jxcsys['uid'];
	    $unm       = $this->jxcsys['name'];
	    $pid = date('Y-m-d').'up'.$uid;	     
	    $ds = $this->mysql_model->get_rows('attend',array('pid'=>$pid));
	    $data['up'] = count($ds) > 0  ? 1 : 0;
        $pid = date('Y-m-d').'dw'.$uid;	     
	    $ds = $this->mysql_model->get_rows('attend',array('pid'=>$pid));
	    $data['dw'] = count($ds) > 0  ? 1 : 0;
        str_alert(200,'success',$data);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */